{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "be83f75b",
   "metadata": {},
   "source": [
    "PyTorch GPU support document\n",
    "\n",
    "- CUDA compatibility: https://pytorch.org/get-started/locally/\n",
    "- CUDA compatibility (previous version): https://pytorch.org/get-started/previous-versions/\n",
    "- CUDA semantics: https://pytorch.org/docs/stable/notes/cuda.html\n",
    "- torch.cuda: https://pytorch.org/docs/stable/cuda.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9f3701f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thu Jul 15 07:53:17 2021       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 460.73.01    Driver Version: 460.73.01    CUDA Version: 11.2     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|                               |                      |               MIG M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  Tesla T4            Off  | 00000000:00:1E.0 Off |                    0 |\n",
      "| N/A   36C    P0    26W /  70W |      0MiB / 15109MiB |      0%      Default |\n",
      "|                               |                      |                  N/A |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                                  |\n",
      "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
      "|        ID   ID                                                   Usage      |\n",
      "|=============================================================================|\n",
      "|  No running processes found                                                 |\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "# Get GPU status with Nvidia System Management Interface (nvidia-smi)\n",
    "# Check driver version and CUDA version are compatible with PyTorch\n",
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76de7171",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nvcc: NVIDIA (R) Cuda compiler driver\n",
      "Copyright (c) 2005-2020 NVIDIA Corporation\n",
      "Built on Wed_Jul_22_19:09:09_PDT_2020\n",
      "Cuda compilation tools, release 11.0, V11.0.221\n",
      "Build cuda_11.0_bu.TC445_37.28845127_0\n"
     ]
    }
   ],
   "source": [
    "# Check the cuda toolkit version\n",
    "! nvcc -V"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "623aa07f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pytorch Version: 1.7.1+cu110\n",
      "CUDA Available: True\n",
      "<torch.cuda.device object at 0x7f32585f55d0>\n",
      "Device Count: 1\n",
      "Device Name: Tesla T4\n"
     ]
    }
   ],
   "source": [
    "# Get PyTorch version and the number of GPUs visible to PyTorch runtime\n",
    "import torch\n",
    "\n",
    "print(\"Pytorch Version:\", torch.__version__)\n",
    "print(\"CUDA Available:\", torch.cuda.is_available())\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    device = torch.cuda.current_device()\n",
    "    print(torch.cuda.device(device))\n",
    "    print('Device Count:', torch.cuda.device_count())\n",
    "    print('Device Name: {}'.format(torch.cuda.get_device_name(device)))\n",
    "else:\n",
    "    device = 'cpu'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8a294b8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[22., 28.],\n",
      "        [49., 64.]], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "# Run 'MatMul' with GPU\n",
    "a = torch.FloatTensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]).to(device)\n",
    "b = torch.FloatTensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).to(device)\n",
    "c = torch.matmul(a, b)\n",
    "\n",
    "print(c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
